from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd

WIDTH = 320
HEIGHT = 640
PIXEL_RATIO = 3.0
UA = "Mozilla/5.0 (Linux; Android 4.1.1; GT-N7100 Build/JRO03C) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/35.0.1916.138 Mobile Safari/537.36 T7/6.3"
mobile_emulation = {
    "deviceMetrics":
        {
            "width": WIDTH,
            "height": HEIGHT,
            "pixelRatio": PIXEL_RATIO
        },
    "userAgent": UA
}
url = "https://m.weibo.cn/u/2684793101?topnav=1&topnav=1&wvr=6&wvr=6&topsug=1&topsug=1&is_hot=1&is_hot=1&jumpfrom=weibocom#_loginLayer_1582272422609"
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_experimental_option('mobileEmulation', mobile_emulation)
chrome_options.add_argument('--disable-gpu')
executable_path = r"..\..\chromedriver.exe"
driver = webdriver.Chrome(executable_path=executable_path, options=chrome_options)
driver.implicitly_wait(10)
driver.get(url)
stopwords = ['\n附「韩红爱心公益事业票据申请表」👉🏻网页链接', '[加油]', ' 第二阶段已正式开启']
links = []
# not more posts were loaded - exit the loop
for i in range(6, 40):
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    xpath = "/html/body/div/div[1]/div[1]/div[%d]/div/div/article/div/div[1]/a[2]/span" % i
    try:
        element = WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.XPATH, xpath)))
    except Exception:
        continue
    else:
        img_name = '//*[@id="app"]/div[1]/div[1]/div[%d]/div/div/article/div/div[1]' % i
        img_path = '//*[@id="app"]/div[1]/div[1]/div[%d]/div/div/article/div/div[2]/div/div/img' % i
        name = WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.XPATH, img_name))).text
        path = WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.XPATH, img_path))).get_attribute("src")
        path = 'https://wx4.sinaimg.cn/large' + path[len('https://wx4.sinaimg.cn/large/'):]
        name = name[len('#韩红爱心驰援武汉##抗击新型肺炎我们在行动# '):]
        for word in stopwords:
            name = name.replace(word, '')
        links.append([name, path])
df = pd.DataFrame(data=links, columns=['name', 'path'])
df.to_csv('image_urls@20200321.csv', index=False)
